// 该文件专门用于创建整个应用的路由器
import VueRouter from "vue-router";
import About from "@/pages/About.vue";
import Home from "@/pages/Home.vue";
import News from "@/pages/News.vue";
import Message from "@/pages/Message.vue";
import Detail from "@/pages/Detail.vue";
//创建并暴露一个路由器
const router = new VueRouter({
  routes: [
    {
      path: '/about',
      component: About,

    },
    {
      name: 'zhuye',
      path: '/home',
      component: Home,
      //   二级路由
      children: [{
        path: 'news',  //此处一定不要写成/news
        component: News,
        // 路由元信息，程序员自定义内容
        meta: {isAuth: true, title: '新闻'},
        // 独享路由守卫只有前置没有后置
        beforeEnter: (to, from, next) => {
          if (to.meta.isAuth) {
            if (localStorage.getItem('school') === 'jin') {
              next()
            } else {
              alert('无权限')
            }
          } else {
            next()
          }
        }
      }, {
        path: 'message',
        component: Message,
        // 路由元信息，程序员自定义内容
        meta: {isAuth: true, title: '消息'},
        children: [
          {
            name: 'xiangqing',
            path: 'detail/:id/:title',
            component: Detail,
            props(route) {
              return {
                id: route.query.id,
                title: route.query.title
              }
            }

          }
        ]
      }]
    }
  ]
})

// 全局前置路由守卫——初始化的时候被调用、每次路由切换之前被调用
/*router.beforeEach((to, from, next) => {
  console.log('@@@@@')
  // 判断是否需要鉴权
  if (to.meta.isAuth) {
    if (localStorage.getItem('school') === 'jin') {
      next()
    } else {
      alert('无权限')
    }
  } else {
    next()
  }
})*/

// 全局后置路由守卫——初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to, from) => {

  // 修改title
  document.title = to.meta.title || '学习Vue'
})

export default router

